<?php require_once(BASEPATH.'application/controllers/validaciones.php');
//Cliente
class guias extends validaciones {
	
	var $titulo;

	function guias(){
		parent::Controller(); 
		$this->load->library("rapyd");
		$this->load->library('pi18n');
		$this->titulo=$this->datasis->traevalor('TITULO4');
	}

	function index(){
		redirect("ventas/guias/filteredgrid");
	}
	function filteredgrid(){
		
		$this->rapyd->load("datafilter","datagrid2");
		$this->rapyd->uri->keep_persistence();
		
		$filter = new DataFilter("Buscar y Filtrar Movimientos de Bancos");
        $select=array( "a.id", "a.factura", "a.fecha", "a.usuario", "a.estampa", "a.nguia", "a.ftraslado", "a.destinatario","a.rucd","a.flete");
		$filter->db->select($select);
		$filter->db->from('remision AS a');
        $filter->db->orderby('remision');
        		
        $ano=date("Y");
        $mes=date("m");
                
        $filter->fechad = new dateonlyField('Desde', 'fechad','d/m/Y');
		$filter->fechah = new dateonlyField('Hasta', 'fechah','d/m/Y');
		$filter->fechah->size=$filter->fechad->size=12;
		$filter->fechah->operator='<=';
        $filter->fechad->insertValue = date("Y-m-d", mktime(0, 0, 0,$mes, 1, $ano));
        $filter->fechah->insertValue = date("Y-m-d"); 
		$filter->fechad->clause  =$filter->fechah->clause ='where';
		$filter->fechad->db_name =$filter->fechah->db_name='a.fecha';
		$filter->fechad->operator='>=';
        
        $filter->formap = new dropdownField("Tipo", "a.formap");
        $filter->formap->option("","Todos");
		$filter->formap->style='width:300px;';
        $filter->formap->options("SELECT formap,nombre FROM formap ORDER BY nombre");
        
        $filter->banco = new dropdownField("Banco", "a.banco");
		$filter->banco->style='width:300px;';
        $filter->banco->option("","Todos");
		$filter->banco->options("SELECT banco, nombre FROM bancos WHERE tipo='B' ORDER BY nombre" );
		$filter->banco->group = "Informaci&oacute;n de Recepci&oacute;n";
        
        $filter->cajero = new dropdownField("Cajero", "a.cajero");
		$filter->cajero->style='width:300px;';
        $filter->cajero->option("","Todos");
		$filter->cajero->options("SELECT cajero, nombre FROM cajero ORDER BY nombre" );
		$filter->cajero->group = "Informaci&oacute;n de Recepci&oacute;n";
        	
		$filter->buttons("reset","search");
		$filter->build("dataformfiltro");
        
        $uri = anchor("ventas/guias/dataedit/show/<#id#>","<#id#>");
        
		$grid = new DataGrid2("Resultado");
		$grid->order_by("fecha","ASC");
        $grid->agrupar(" ",'nombre');
        $grid->use_function('tipo');
		$grid->per_page=10;
		
        $grid->column("Fecha"               ,"<dbdate_to_human><#fecha#></dbdate_to_human>"  ,"align='center'");
        $grid->column("N&uacute;mero"       ,$uri                                            ,"align='center'");
        $grid->column("Tipo"                ,"nformap"                                       ,"align='left'");
        //$grid->column("Del Banco"           ,"tbanco"                                        ,"align='left'");
        $grid->column("N&uacute;mero"       ,"referencia"                                    ,"align='left'");
        $grid->column("Monto"               ,"<nformat><#monto#></nformat>"                   ,"align='right'");
        $grid->column("Cajero"              ,"cajero"                                         ,"align='center'");
        $grid->column("Recepcion"           ,"<dbdate_to_human><#recepcion#></dbdate_to_human>"  ,"align='center'");
        $grid->column("Conciliado"          ,"conciliado"                                     ,"align='center'");
        $grid->column("Observaci&oacute;n"  ,"observacion"                                    ,"align='left'");
        
		$grid->totalizar('monto');
        
		$grid->add("ventas/guias/dataedit/create","Nuevo Deposito");
		$grid->build('datagridST');
		//echo $grid->db->last_query();
		
		$cana=$grid->recordCount;
		$grid=$grid->output;
		
		$extras = '
		<script type="text/javascript">
		//<![CDATA[
		(function() {
			var mySt = new superTable("demoTable", {
			cssSkin : "sSky",
			fixedCols : 1,
			headerRows : 1,
			onStart : function () {	this.start = new Date();},
			onFinish : function () {document.getElementById("testDiv").innerHTML += "Finished...<br>" + ((new Date()) - this.start) + "ms.<br>";}
			});
		})();
		//]]>
		</script>
		';
		
		$style ='
		<style type="text/css">
		.fakeContainer { /* The parent container */
		    margin: 5px;
		    padding: 0px;
		    border: none;
		    width: 1000px; /* Required to set */
		    height: 320px; /* Required to set */
		    overflow: hidden; /* Required to set */
		}
		</style>	
		';

		$data['style']   = $style;
		$data['style']  .= style('superTables.css');

		$data['extras']  = $extras;		
		//echo $cana;
		
			IF($cana<=0){
				$data['content'] = '<div align="center">Ningun Resultado Obtenido</div>';	
			}ELSE{
				$data['content'] = $grid;		
			}

		//$data['content'] = $grid;
	
		$data['content'].= $this->pi18n->fallas();
		$data['filtro']  = $filter->output;

		$data['title']   = heading("Movimiento de Bancos");
		$data['script']  = script('jquery.js');
		$data["script"] .= script('superTables.js');
		$data['head']    = $this->rapyd->get_head();

		$this->load->view('view_ventanas', $data);
	}
    function dataedit(){
		
		$this->rapyd->load("dataedit");				
		$edit = new DataEdit(" ", "guias");
		
		$edit->back_url = site_url("ventas/guias/filteredgrid");
		    
		//echo $abono;
				
		$script ='
		$(function() {
			$(".inputnum").numeric(".");
		});';
		
		$edit->script($script, "create");
		$edit->script($script, "modify");
		
        //$edit->post_process('update','_post_update');
		        
        $edit->usuario  = new autoUpdateField('usuario', $this->session->userdata('usuario'), $this->session->userdata('usuario'));
		
        $edit->id = new hiddenField(" ", 'id');
        $id=$edit->_dataobject->get("id");
        
        $edit->fecha = new DateonlyField('Fecha', 'fecha','d/m/Y');
		$edit->fecha->rule = 'required';
		$edit->fecha->size = 12;
		$edit->fecha->group="Informaci&oacute;n del Movimiento";
		                      				
		$edit->formap = new dropdownField("Tipo", "formap");
		$edit->formap->style='width:300px;';
        //$edit->formap->option("DC","DEPOSITO DE CHEQUES");
        $edit->formap->options("SELECT formap,nombre FROM formap ORDER BY nombre");
		$edit->formap->group = "Informaci&oacute;n del Movimiento";

       $edit->banco = new dropdownField("Ingreso al Banco", "banco");
		$edit->banco->style='width:300px;';
		$edit->banco->options("SELECT banco, nombre FROM bancos ORDER BY nombre" );
		$edit->banco->group = "Informaci&oacute;n del Movimiento";
            
        //$edit->tbanco = new dropdownField("Del Banco", "tbanco");
	//	$edit->tbanco->style='width:300px;';
	//	$edit->tbanco->options("SELECT banco, nombre FROM tbancos ORDER BY nombre" );
	//	$edit->tbanco->group = "Informaci&oacute;n del Movimiento";
		
		$edit->referen = new inputField('N&uacute;mero de Referencia', 'referencia');
		$edit->referen->size = 40;
		$edit->referen->rule = 'required';
		$edit->referen->maxlength=50;
		$edit->referen->group="Informaci&oacute;n del Movimiento";
        
        $edit->monto = new inputField("Monto", 'monto');
        $edit->monto->css_class ='inputnum';
		$edit->monto->size      = 20;
		$edit->monto->rule = 'required';
        $edit->monto->group="Informaci&oacute;n del Movimiento";
        
        $edit->recepcion = new DateonlyField('Fecha de Entrega', 'recepcion','d/m/Y');
		$edit->recepcion->insertValue = date('d/m/Y');
		//$edit->recepcion->rule = 'required';
		$edit->recepcion->size = 12;
		$edit->recepcion->group="Informaci&oacute;n de Recepci&oacute;n";
                        
        
        $edit->cajero = new dropdownField("Recibe Cajero", "cajero");
		$edit->cajero->style='width:300px;';
		$edit->cajero->options("SELECT cajero, nombre FROM cajero ORDER BY nombre" );
		$edit->cajero->group = "Informaci&oacute;n de Recepci&oacute;n";
       
        $formap=$edit->_dataobject->get("formap");
        //ECHO $formap;
        if($formap=='DC'){
             
            $edit->cheques = new containerField('cheques',$this->_detalle($id));
            $edit->cheques->when = array("show");
            $edit->cheques->group="Informaci&oacute;n de Cheques Depositados";
        }
        
            
        $edit->idpagos = new hiddenField(" ", 'idpagos');
        $idpagos=$edit->_dataobject->get("idpagos");
            
        if($formap!='DC'){ 
            $edit->pagos = new containerField('pagos',$this->_detalle2($idpagos));
            $edit->pagos->when = array("show");
            $edit->pagos->group="Relaci&oacute;n con Abono";
        }
       
		$edit->buttons("back","save","modify","undo");
		$edit->build();
        
        $propiedad = array('src' => 'images/imprime.png', 'alt' => 'Imprimir', 'title' => 'Imprimir','border'=>'0','height'=>'80');		
        
        IF(empty($idpagos)){
          $imprime='<a href='.site_url("formatos/ver/DEPOSITOS/$id").'>'.img($propiedad).'</a>';
        }else{
          $imprime='<a href='.site_url("formatos/ver/LETRA/$idpagos").'>'.img($propiedad).'</a>';      
        }
        
        $data['content']=' ';
        
        if($edit->_status=='show'){
            $data['content'] .=$imprime; 
        }
        
        $data['content'] .=$edit->output; 
               
		$data['title']   =  heading("Movimiento de Banco");
		$data["head"]    = script("jquery.pack.js").script("plugins/jquery.numeric.pack.js").script("plugins/jquery.floatnumber.js").$this->rapyd->get_head();
		$this->load->view('view_ventanas', $data);
			
	}
    function cheques($estado='',$id=''){
        
        $mSCLId=array(
		'tabla'   =>'scli',
		'columnas'=>array(
		'cliente' =>'Cliente',
		'nombre'=>'Nombre', 
		'rifci'=>'Rif/Cedula',
		'dire11'=>'Direcci&oacute;n'),
		'filtro'  =>array('nombre'=>'Nombre'),
		'retornar'=>array('cliente'=>'cod_cli','nombre'=>'nombre'),
		'titulo'  =>'Buscar Cliente');
		$boton =$this->datasis->modbus($mSCLId);
		
		$this->rapyd->load("dataedit");				
		$edit = new DataEdit("Informaci&oacute;n del Cheque ", "cheques");
		
         if($edit->_status=='show'){
            
            $id=$edit->_dataobject->get("idguias");
            $edit->back_url = site_url("ventas/guias/dataedit/show/$id");
        }else{
            
            $edit->back_url = site_url("ventas/guias/dataedit/show/$id");
        }       
		
        		
        $script ='
		$(function() {
			$(".inputnum").numeric(".");
		});';
		
		$edit->script($script, "create");
		$edit->script($script, "modify");
		
        //$edit->post_process('update','_post_update');
		        
        $edit->idguias = new hiddenField(" ", 'idguias');
        $edit->idguias->insertValue =$id;
        
        $edit->fecha = new DateonlyField('Fecha de Cheque', 'fecha','d/m/Y');
		$edit->fecha->rule = 'required';
		$edit->fecha->size = 12;

        $edit->cliente = new inputField('Codigo','cod_cli');
		$edit->cliente->size = 6;
		$edit->cliente->maxlength=5;
		$edit->cliente->append($boton);
		$edit->cliente->readonly  = true;
				
		$edit->nombre = new inputField('Nombre', 'nombre');
		$edit->nombre->size = 35;
		$edit->nombre->maxlength=40;
		$edit->nombre->autocomplete=false;
		$edit->nombre->rule= 'required';
		$edit->nombre->readonly  = true;
            
        $edit->tbanco = new dropdownField("Del Banco", "tbanco");
		$edit->tbanco->style='width:300px;';
		$edit->tbanco->options("SELECT banco, nombre FROM tbancos ORDER BY nombre" );
		
		$edit->numero = new inputField('N&uacute;mero de Cheque', 'numero');
		$edit->numero->size = 40;
		$edit->numero->rule = 'required';
		$edit->numero->maxlength=50;
        
        $edit->monto = new inputField("Monto", 'monto');
        $edit->monto->css_class ='inputnum';
		$edit->monto->size      = 20;
		$edit->monto->rule = 'required';
        
		$edit->buttons("back","save","modify","undo");
		$edit->build();
               
		$data['content'] =$edit->output;               
		$data['title']   =  heading("Cheques Depositados");
		$data["head"]    = script("jquery.pack.js").script("plugins/jquery.numeric.pack.js").script("plugins/jquery.floatnumber.js").$this->rapyd->get_head();
		$this->load->view('view_ventanas', $data);
			
	}
    
    //muestra cheques depositados
    
    function _detalle($id){
  	$salida='No se asignado cheques depositados';
  	if(!empty($id)){
  		$this->rapyd->load('datagrid2');
  					
			$propiedad = array('src' => 'images/agregar.jpg', 'alt' => 'Nueva Cuota', 'title' => 'Nueva Cuota','border'=>'0','height'=>'20');		
			$add='<a href='.site_url("ventas/guias/cheques/create/$id").'>'.img($propiedad).'</a>';
			
			$grid = new DataGrid2($add.' <-- Agregar Cheque');		
			$select=array("id","fecha","monto","numero","cod_cli","a.nombre","idguias","b.nombre as tbanco");
			$grid->db->select($select);
			$grid->db->from('cheques as a');
            $grid->db->join('tbancos AS b' ,'a.tbanco=b.banco');	
			$grid->db->where('idguias',$id);
			
			$uri = anchor("ventas/guias/cheques/show/<#id#>",'<#numero#>');
            $uri2 = anchor("ventas/guias/eliminacheque/<#id#>/<#idguias#>",'Eliminar');
						
            $grid->column("Fecha"               ,"<dbdate_to_human><#fecha#></dbdate_to_human>"  ,"align='center'");
            $grid->column("Cliente"             ,"cod_cli"                                       ,"align='left'");
            $grid->column("Nombre"              ,"nombre"                                        ,"align='left'");
            $grid->column("Del Banco"           ,"tbanco"                                        ,"align='left'");
            $grid->column("N&uacute;mero"       ,$uri                                            ,"align='left'");
            $grid->column("Monto"               ,"<nformat><#monto#></nformat>"                  ,"align='right'");
			$grid->column(" "                   ,$uri2,"align='left'");
			
			$grid->totalizar('monto');
			$grid->build();
			//$grid->build('datagridST');
			$salida=$grid->output;
			
			//Echo $grid->db->last_query(); 
		}
		return $salida;
	}
   
    //relacion con abono
    
    function _detalle2($id){
  	$salida=' ';
  	if(!empty($id)){
  		$this->rapyd->load('datagrid2');
  					
            $grid = new DataGrid2(" ");
            $select=array('a.id','a.cod_cli','b.nombre','a.factura','a.id','a.ncuota','monto',
            'usuario','estampa','fecha','dias','a.observa');
            $grid->db->select($select);
            $grid->db->from('itpagos AS a');
            $grid->db->join('scli AS b' ,'a.cod_cli=b.cliente');
            $grid->db->where("a.id",$id);
            $grid->per_page=10;  
            
            $grid->column('Factura'                  ,"factura"                                        ,"align='center'");
            $grid->column('N&deg Letra'              ,"ncuota"                                        ,"align='center'");
            $grid->column("Cliente"                  ,"cod_cli"                                       ,"cliente");
            $grid->column("Nombre"                   ,"nombre"                                        ,"align='LEFT'");
            $grid->column("Dias"                     ,"dias"                                          ,"align='center'");
            $grid->column("Cancelar el d&iacute;a"   ,"<dbdate_to_human><#fecha#></dbdate_to_human>"  ,"align='center'");
            $grid->column("Observaci&oacute;n"       ,"observa"                                       ,"align='left'");
            
			$grid->build();
			//$grid->build('datagridST');
			$salida=$grid->output;
			
			//Echo $grid->db->last_query(); 
		}
		return $salida;
	}
    
    //elimina cheque
    
    function eliminacheque($id='',$idguias=''){
        
        $mSQL=$this->db->query("DELETE FROM cheques WHERE id='$id'");
        redirect("ventas/guias/dataedit/show/$idguias");
        
    }
}
?>
